<template>
<div>
<div class="block">
    <p>使用 scoped slot</p>
    <el-tree
      :data="data"
      node-key="id"
      default-expand-all
      @node-drop="handleDrop"
      draggable
      :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label}}</span>
        <span>
          <el-button
            type="text"
            size="mini"
            class='el-icon-plus'
            @click="() => append(data)">
          </el-button>
          <el-button
            type="text"
            size="mini"
            class='el-icon-delete'
            @click="() => remove(node, data)">
          </el-button>
        </span>
         <!-- :class="{'white': node.parent.parent || node.parent.data.children  ? node.parent.data.children[0].id == data.id : false }" -->
      </span>
    </el-tree>
  </div>
  
</div>
</template>
<script>
  let id = 1000;
  export default {
    data() {
      const data = [{
        id: 1,
        label: '一级 1',
        children: [{
          id: 4,
          label: '二级 1-1',
          children: [{
            id: 9,
            label: '三级 1-1-1'
          }, {
            id: 10,
            label: '三级 1-1-2'
          }]
        }]
      }, {
        id: 2,
        label: '一级 2',
        children: [{
          id: 5,
          label: '二级 2-1'
        }, {
          id: 6,
          label: '二级 2-2'
        }]
      }, {
        id: 3,
        label: '一级 3',
        children: [{
          id: 7,
          label: '二级 3-1'
        }, {
          id: 8,
          label: '二级 3-2'
        }]
      }];
      return {
        data: JSON.parse(JSON.stringify(data)),
      }
    },
    methods: {
      handleDrop(draggingNode, dropNode, dropType, ev) {
        console.log('tree drop: ', dropNode.label, dropType);
        console.log('移动后的数据',this.data);
      },
      append(data) {
          this.$prompt('请输入目录名称', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
          inputErrorMessage: '不能为空'
        }).then(({ value }) => {
            const newChild = { id: id++, label: value, children: [] };
            if (!data.children) {
            this.$set(data, 'children', []);
            }
            data.children.push(newChild);
            console.log('打印一下添加后的数据',this.data);
        }).catch(() => {
                
        });
      },
      remove(node, data) {
        const parent = node.parent;
        const children = parent.data.children || parent.data;
        const index = children.findIndex(d => d.id === data.id);
        children.splice(index, 1);
      }
    }
  };
</script>

<style>
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
  .white{
      color:#f2f3f3 !important;
      cursor: not-allowed !important
  }
</style>